/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wstrader.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kiendn
 */
public class Common {
    
    public static final String time_format = "hh:mm:ss";
    
    public static final String date_format = "yy-MM-dd";

    static private final String[] Months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    
    public static Date getDateType(String date, String format) {
        DateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException ex) {
            Logger.getLogger(Common.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public static String toSqlDate(String date) {
        String[] new_string = date.split("-");
        if (new_string.length == 3) {
            String day = "";
            if (Integer.parseInt(new_string[0]) < 10) {
                day = "0" + new_string[0];
            } else {
                day = new_string[0];
            }
            String mon = "";
            int a_mon = getMonth(new_string[1]);
            if (a_mon < 10){
                mon = "0" + a_mon;
            }else{
                mon = "" + a_mon;
            }

            String year = "20" + new_string[2];
            
            return year + "-" + mon + "-" + day;
        }
        return null;
    }    
    
    
    public static int getMonth(String month){
        for (int i=0; i<Months.length; i++){
            if (month.equals(Months[i])){
                return ++i;
            }
        }
        return 0;
    }
    
    public static void main(String[] args){
        System.out.println(Common.getDateType("2011-12-10", date_format));
    }
}
